/**
 * 广告支出-按广告名称统计
 */   
$(function(){
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}
	$(".pageBox button").css("background-color","#478df1");
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
			}else{
				if(page==1){
					var adName = $("#adName").val();
					totalByAdName(page,adName);
				}else if(page>showPageN-4){
					
				}else{
					totalByAdName0(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
});

function totalByAdName0(page){
	var startTime;
	var endTime;
	var adName = $("#adName").val();
	$.ajax({
		url : '/Lvscore_Advertiser_Maven/byTotalAdname',
        data:{"startTime":startTime,"endTime":endTime,"page":page,"adName":adName},
		type : 'post',
		success : function(data) {
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);	
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	totalByAdName2(page);
	            }
			})
		},
        error: function(){}        
	});
}
//加载页面触发的事件 展示按广告名称统计的数据
	function totalByAdName(page,adName){
		if(page==null || page.length<=0){
			var page=1;
		}
		var startTime;
		var endTime;
			$.ajax({
				url : '/Lvscore_Advertiser_Maven/byTotalAdname',
		        data:{"startTime":startTime,"endTime":endTime,"page":page,"adName":adName},
				type : 'post',
				success : function(data) {
					$('#totalAdName').find("tr").eq(0).siblings("tr").remove();
					$("#endTime").val(data.endTime);
					$("#startTime").val(data.startTime);	
					$('#pageBox').extendPagination({
						totalCount:data.total,
				 		showPage: data.pages+1,
			            limit: data.pageSize,
			            goPage:page,
			            callback: function (page) {
			            	totalByAdName2(page);
			            }
					});
					
					var MyPays=0;	
					var beans = data.beans;
					var proportions=0;
					var recharges=0;
					var adBudgets=0;
					var clickNums=0;
				
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					if(beans[i].device=="pc"){
						beans[i].device="PC";
					}else if(beans[i].device=="mobile"){
						beans[i].device="手机";
					}
					if(beans[i].adForm=="video"){
						beans[i].adForm="视频";
					}else if(beans[i].adForm=="picture"){
						beans[i].adForm="图片";
					}
					var tbody = "<tr><td>"+(i+1)+"</td>" +
					"<td>"+beans[i].adName +"</td>" +
					"<td>"+beans[i].adNum+"</td>" +
					"<td>"+beans[i].industry+"</td>" +
					"<td>"+beans[i].adForm+"</td>" +
					"<td>"+beans[i].device+"</td>" +
					"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
					"<td>"+beans[i].clickNum+"</td>" +
					"<td>"+beans[i].adPay+"</td>" +
					"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
					"<td>"+beans[i].adBudget+"</td>" +
					"<td>"+((page-1)*5+(i+1))+"</td>"+
					"<td><a class='btn btn-primary' href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdNameOneday.html?selectedNav=3&navPos=1&endTime="+data.endTime+"&startTime="+data.startTime+"&adName="+beans[i].adName+"&adNumber="+beans[i].adNum+"'>查看</a></td>"+"</tr>"
					
					$('#totalAdName').append(tbody);
				}
				
				var allBean=data.allBean;
				for (var i = 0; i < allBean.length;i++) {
					MyPays+=allBean[i].adPay*1;
					var proportion=(allBean[i].proportion*100).toFixed(2);
					proportions+=proportion*1;
					adBudgets+=allBean[i].adBudget*1;
					clickNums+=allBean[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" +
			    "<td>---</td>" + 
			 "</tr>";
				if(data.pages==data.pageNum){
					$('#totalAdName').append(tbodyTwo);
				}
				
			},
	        error: function(){}        
		});
	}
	//为了分页做的事件
	function totalByAdName2(page){
		var endTime = $("#endTime").val();
		var startTime=$("#startTime").val();
		var adName = $("#adName").val();
			$.ajax({
				url : '/Lvscore_Advertiser_Maven/byTotalAdname',
		        data:{"startTime":startTime,"endTime":endTime,"page":page,"adName":adName},
				type : 'post',
				success : function(data) {
					$('#totalAdName').find("tr").eq(0).siblings("tr").remove();
					$("#endTime").val(data.endTime);
					$("#startTime").val(data.startTime);
					var a=$("#startTime").val(data.startTime);
					var b=$("#endTime").val(data.endTime);
	
					var MyPays=0;	
					var beans = data.beans;
					var proportions=0;
					var recharges=0;
					var adBudgets=0;
					var clickNums=0;
					//循环输出表格
					for(var i = 0; i < beans.length;i++){
						if(beans[i].device=="pc"){
							beans[i].device="PC";
						}else if(beans[i].device=="mobile"){
							beans[i].device="手机";
						}
						
						if(beans[i].adForm=="video"){
							beans[i].adForm="视频";
						}else if(beans[i].adForm=="picture"){
							beans[i].adForm="图片";
						}
						var tbody = "<tr><td>"+(i+1)+"</td>" +
						"<td>"+beans[i].adName +"</td>" +
						"<td>"+beans[i].adNum+"</td>" +
						"<td>"+beans[i].industry+"</td>" +
						"<td>"+beans[i].adForm+"</td>" +
						"<td>"+beans[i].device+"</td>" +
						"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
						"<td>"+beans[i].clickNum+"</td>" +
						"<td>"+beans[i].adPay+"</td>" +
						"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
						"<td>"+beans[i].adBudget+"</td>" +
						"<td>"+((page-1)*5+(i+1))+"</td>"+
						"<td><a class='btn btn-primary' href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdNameOneday.html?selectedNav=3&navPos=1&endTime="+data.endTime+"&startTime="+data.startTime+"&adName="+beans[i].adName+"&adNumber="+beans[i].adNum+"'>查看</a></td>"+"</tr>"
						
						$('#totalAdName').append(tbody);
						/*MyPays+=beans[i].adPay*1;
						var proportion=(beans[i].proportion*100).toFixed(2);
						proportions+=proportion*1;
						adBudgets+=beans[i].adBudget*1;
						clickNums+=beans[i].clickNum*1;	*/
					}
					var allBean=data.allBean;
					for (var i = 0; i < allBean.length;i++) {
						MyPays+=allBean[i].adPay*1;
						var proportion=(allBean[i].proportion*100).toFixed(2);
						proportions+=proportion*1;
						adBudgets+=allBean[i].adBudget*1;
						clickNums+=allBean[i].clickNum*1;
					}
					var tbodyTwo="<tr>" +
					"<td>总计</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>"+clickNums+"</td>" +
					"<td>"+MyPays.toFixed(2)+"</td>" +
				    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
				    "<td>"+adBudgets.toFixed(2)+"</td>" +
				    "<td>---</td>" +
				    "<td>---</td>" + 
				 "</tr>";
					if(data.pages==data.pageNum){
						$('#totalAdName').append(tbodyTwo);
					}
				},
		        error: function(){}        
			});
	}

//点击查询说触发的时间 按时间和名称查询数据
function BytotalAdName(startTime,endTime,adName){
	if(page==null || page.length<=0){
		var page=1;
	}
	$.ajax({
		url : '/Lvscore_Advertiser_Maven/byTotalAdname',
        data:{"startTime":startTime,"endTime":endTime,"page":page,"adName":adName},
		type : 'post',
		success : function(data) {
			$('#totalAdName').find("tr").eq(0).siblings("tr").remove();
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);	
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	totalByAdName2(page);
	            }
			});
			
			var MyPays=0;	
			var beans = data.beans;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			var clickNums=0;
		//循环输出表格
		for(var i = 0; i < beans.length;i++){
			if(beans[i].device=="pc"){
				beans[i].device="PC";
			}else if(beans[i].device=="mobile"){
				beans[i].device="手机";
			}
			if(beans[i].adForm=="video"){
				beans[i].adForm="视频";
			}else if(beans[i].adForm=="picture"){
				beans[i].adForm="图片";
			}
			var tbody = "<tr><td>"+(i+1)+"</td>" +
			"<td>"+beans[i].adName +"</td>" +
			"<td>"+beans[i].adNum+"</td>" +
			"<td>"+beans[i].industry+"</td>" +
			"<td>"+beans[i].adForm+"</td>" +
			"<td>"+beans[i].device+"</td>" +
			"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
			"<td>"+beans[i].clickNum+"</td>" +
			"<td>"+beans[i].adPay+"</td>" +
			"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
			"<td>"+beans[i].adBudget+"</td>" +
			"<td>"+((page-1)*5+(i+1))+"</td>"+
			"<td><a class='btn btn-primary' href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdNameOneday.html?selectedNav=3&navPos=1&endTime="+data.endTime+"&startTime="+data.startTime+"&adName="+beans[i].adName+"&adNumber="+beans[i].adNum+"'>查看</a></td>"+"</tr>"
			
			$('#totalAdName').append(tbody);
			/*MyPays+=beans[i].adPay*1;
			var proportion=(beans[i].proportion*100).toFixed(2);
			proportions+=proportion*1;
			adBudgets+=beans[i].adBudget*1;
			clickNums+=beans[i].clickNum*1;*/
		}
		var allBean=data.allBean;
		for (var i = 0; i < allBean.length;i++) {
			MyPays+=allBean[i].adPay*1;
			var proportion=(allBean[i].proportion*100).toFixed(2);
			proportions+=proportion*1;
			adBudgets+=allBean[i].adBudget*1;
			clickNums+=allBean[i].clickNum*1;
		}
		var tbodyTwo="<tr>" +
		"<td>总计</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>"+clickNums+"</td>" +
		"<td>"+MyPays.toFixed(2)+"</td>" +
	    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
	    "<td>"+adBudgets.toFixed(2)+"</td>" +
	    "<td>---</td>" +
	    "<td>---</td>" + 
	 "</tr>";
		if(data.pages==data.pageNum){
		$('#totalAdName').append(tbodyTwo);
		}
	},
    error: function(){}        
});
}